//
//  VCPunchList.m
//  MOSAD_HW2_1
//
//  Created by Jason on 2016/10/29.
//  Copyright © 2016 Jason. All rights reserved.
//

#import "VCPunchList.h"

@interface VCPunchList ()

@end

@implementation VCPunchList

- (instancetype)init
{
    self = [super init];
    
    UIImage * image = [UIImage imageNamed:@"find.png"];
    //修改图片大小
    UIGraphicsBeginImageContext(CGSizeMake(32, 32));
    [image drawInRect:CGRectMake(0, 0, 32, 32)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //创建一个分栏按钮对象
    UITabBarItem * tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:reSizeImage tag:1];
    
    self.tabBarItem = tabBarItem;

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.translucent = NO;
    //设置背景颜色
    self.view.backgroundColor = [UIColor whiteColor];
    
    //修改导航栏文字
    [self.navigationItem setTitle:@"打卡清单"];
    
    //添加背景
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)[UIColor grayColor].CGColor, (__bridge id)[UIColor whiteColor].CGColor];
    //gradientLayer.locations = @[@0.3, @0.5, @1.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 1.0);
    gradientLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    [self.view.layer addSublayer:gradientLayer];
    
    
    
    //打卡显示区
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height-240) style:UITableViewStylePlain];
    //设置代理
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    //数据视图的头部视图的设定 UIView类型
    _tableView.tableHeaderView = nil;
    //数据视图的尾部视图
    _tableView.tableFooterView = nil;
    
    [self.view addSubview:_tableView];
    
    _tableView.backgroundColor = [UIColor clearColor];
    //去掉多余横线
    UIView * view = [[UIView alloc]init];    
    view.backgroundColor = [UIColor clearColor];
    [_tableView setTableFooterView:view];

    AppDelegate * myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    myDelegate.tableView = _tableView;
    
    //添加搜索栏
    //搜索框
    _searchBar = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, self.view.bounds.size.width-20, 40)];
    _searchBar.placeholder = @"搜索框";
    _searchBar.borderStyle = UITextBorderStyleRoundedRect;
    _searchBar.layer.cornerRadius = 5;
    _searchBar.layer.borderWidth = 1;
    _searchBar.layer.borderColor = [[UIColor blackColor] CGColor];
    [self.view addSubview:_searchBar];
    //设置按键类型
    _searchBar.returnKeyType = UIReturnKeySearch;
    //这里设置为无文字就灰色不可点
    _searchBar.enablesReturnKeyAutomatically = YES;
    _searchBar.delegate = self;
    
    //添加刷新按钮
    UIBarButtonItem * refreshBtn = [[UIBarButtonItem alloc] initWithTitle:@"刷新" style:UIBarButtonItemStyleDone target:self action:@selector(pressRefresh)];
    self.navigationItem.rightBarButtonItem = refreshBtn;
    
    
}

-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //回收键盘对象
    [_searchBar resignFirstResponder];
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    //[textField resignFirstResponder];
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"你还真以为能查呀！" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * OK = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:OK];
    [self presentViewController:alertController animated:YES completion:nil];
    return YES;
}

-(void) pressRefresh {
    [_tableView reloadData];
}




//获得组数，默认section组数返回1
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    AppDelegate * myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSLog(@"myDelegate.publishTimes = %ld", myDelegate.publishTimes);
    return  4 + myDelegate.publishTimes;
}

//每组元素个数，默认为1
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AppDelegate * myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    NSString * str = @"cell";
    
    //尝试获取可以复用的单元格
    //如果得不到，返回为nil
    UITableViewCell * cell = [_tableView dequeueReusableCellWithIdentifier:str];
    
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    
    [cell.textLabel setNumberOfLines:5];
    NSLog(@"section = %ld", indexPath.section);
    cell.textLabel.text = [NSString stringWithFormat:@"日期\t%@\n\n地点\t%@\n\n心得\t%@", myDelegate.time[myDelegate.publishTimes + 3 - indexPath.section], myDelegate.location[myDelegate.publishTimes + 3 - indexPath.section], myDelegate.experience[myDelegate.publishTimes + 3 - indexPath.section]];
    
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.layer.borderWidth = 1;
    cell.textLabel.layer.cornerRadius = 5;
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.backgroundColor = [UIColor whiteColor];
    //是否进行剪切
    cell.textLabel.clipsToBounds = YES;
    //cell.contentView.backgroundColor = [UIColor greenColor];
    
    //被选中时的颜色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

//单元格的高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

////获取每组头部标题
//-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    return @".";
//}

//获取每组头部高度
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 18;
}

//修改头部标题颜色
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 60)];
    headerView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    return headerView;
}

//选中单元格调用此协议函数
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NSLog(@"选中单元格 %ld, %ld", indexPath.section, indexPath.row);
    AppDelegate * myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    VCCheckPunch * checkPunch = [[VCCheckPunch alloc] init];
    checkPunch.num = myDelegate.publishTimes + 3 - indexPath.section;
    [self.navigationController pushViewController:checkPunch animated:YES];
}

//给cell添加动画
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //设置Cell的动画效果为3D效果
    //设置x和y的初始值为0.1；
    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
    //x和y的最终值为1
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
    }];
}

////设置cell的显示动画为3D缩放
////xy方向缩放的初始值为0.1
//cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
//
////设置动画时间为0.25秒,xy方向缩放的最终值为1
//[UIView animateWithDuration:0.25 animations:^{
//
//    cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
//
//}completion:^(BOOL finish){
//
//}];

//-(void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//    CATransform3D rotation;
//    rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
//    rotation.m34 = 1.0/ -600;
//
//    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
//    cell.layer.shadowOffset = CGSizeMake(10, 10);
//    cell.alpha = 0;
//    cell.layer.transform = rotation;
//    cell.layer.anchorPoint = CGPointMake(0, 0.5);
//
//
//    [UIView beginAnimations:@"rotation" context:NULL];
//    [UIView setAnimationDuration:0.8];
//    cell.layer.transform = CATransform3DIdentity;
//    cell.alpha = 1;
//    cell.layer.shadowOffset = CGSizeMake(0, 0);
//    [UIView commitAnimations];
//}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
